﻿using System.Collections.Generic;
using System.Linq;
using Socium.Domain.Collections;
using Socium.Domain.Persons;
using Socium.Domain.Tests;
using Socium.Domain.Tests.Testings;

namespace Socium.Testings {
	class SingleQuestionTreeItem : QuestionTreeItemBase<SingleVariantTreeItem> {
		public SingleQuestionTreeItem(ClosedQuestion model) : base(model) {}

		SingleVariantTreeItem _selected_variant;

		public SingleVariantTreeItem SelectedVariant {
			get { return _selected_variant; }
			set {
				_selected_variant = value;
				OnPropertyChanged("SelectedVariant");
			}
		}

		protected override IEnumerable<SingleVariantTreeItem> GetChilderen(ClosedQuestion model) {
			return model.Variants.Select(variant => new SingleVariantTreeItem(variant));
		}

		public override void Save(Person person, Testing testing) {
			testing.RemoveAnswers(person, Model);
			if (_selected_variant != null) {
				_selected_variant.Save(person, testing);
			}
		}

		public override void Load(Person person, Testing testing) {
			var answer = testing.FindAnswer(person, Model) as ClosedAnswer;
			SelectedVariant = answer == null ? null : Childeren.Find(item => item.IsMatch(answer.Variant), null);
		}
	}
}
